cd "C:\Users\pgi1\Dropbox\Coal 2021 _ Joe Peter\Bank Regressions\data"

use  matched_dataset_annual, clear
bysort ultimateparentid year: keep if _n==1

destring ultimateparentid, force replace
bysort ultimateparentid year: keep if _n==1
xtset ultimateparentid year

foreach var of varlist deal_amount_mtr loans_mtr deal_amount_total loans_total deal_amount_related loans_related {
	gen `var'_lag3=(L.`var'+L2.`var'+L3.`var')/3
}

gen treated=(year>minpolicyyear)
gen treated_project=(year>projectban)
gen treated_company=(year>companyban)
gen treated_enhanced=(year>enhancedreview)




gen pretreated=(year>=minpolicyyear-3 & year<minpolicyyear)
gen pretreated_project=(year>=projectban-3 & year<projectban)
gen pretreated_company=(year>=companyban-3 & year<companyban)
gen pretreated_enhanced=(year>=enhancedreview-3 & year<enhancedreview)

sum

bysort ultimateparentid: egen treated_ever=max(treated)
bysort ultimateparentid: egen treated_project_ever=max(treated_project)
bysort ultimateparentid: egen treated_company_ever=max(treated_company)
bysort ultimateparentid: egen treated_enhanced_ever=max(treated_enhanced)

gen trend=year-2000
gen diff_trend_treated=trend*treated_ever
gen diff_trend_treated_project=trend*treated_project_ever
gen diff_trend_treated_company=trend*treated_ever*treated_company_ever
gen diff_trend_treated_enhanced=trend*treated_enhanced_ever

gen assets_lagged=L.total_assets
gen log_assets=log(L.total_assets)

label var log_assets "Log Assets"
label var asset_growth_3 "Asset Growth"




tab year, gen (y_)

label var pretreated "Before Any Policy"
label var pretreated_company "Before Company Policy"
label var pretreated_project "Before Project Policy"
label var pretreated_enhanced "Before Enhanced Policy"

label var treated "Anti-MTR Policy"
label var treated_company "Anti-MTR Company Policy"
label var treated_project "Anti-MTR Project Policy"
label var treated_enhanced "Anti-MTR Enhanced Policy"
label var loans_related_lag3  "Coal loans, Avg. [t-3,t-1]"
label var loans_total_lag3 "Total loans, Avg. [t-3,t-1]"

label var diff_trend_treated "Differential Trend for Treated"
label var diff_trend_treated_company "Differential Trend for Treated"
label var diff_trend_treated_project "Differential Trend for Treated"
label var diff_trend_treated_enhanced "Differential Trend for Treated"


gen loans_related_lag3_ln=ln(loans_related_lag3+1)
gen loans_total_lag3_ln=ln(loans_total_lag3+1)
gen loans_mtr_ln=ln(loans_mtr+1)
gen loans_mtr_rel_ln=ln(loans_mtr_rel+1)
gen loans_mtr_norel_ln=ln(loans_mtr_norel+1)
gen deal_amount_mtr_ln=ln(deal_amount_mtr/1000000+1)

label var loans_related_lag3_ln  "Ln Coal Loans"
label var loans_total_lag3_ln "Ln Total Loans"

gen treated_deposit=mtrdepositshare*treated
gen treated_articles=relevant_article*treated

label var treated_deposit "Any Policy x Share of Deposits"
label var treated_articles "Any Policy x Articles"


gen treated_assets=log_assets*treated
label var treated_assets "Any Policy x Log Assets"

gen loans_mtr_total=100*loans_mtr/loans_total
label var loans_mtr_total "Ratio of MTR Loans to Total Loans"

gen has_article=(relevant_article>0 )
replace has_article=. if mi(relevant_article)
label var has_article "Media Attention"
gen treated_has_article=has_article*treated
label var treated_has_article "Media Attention x Any Policy"

drop has_deposits
gen has_deposits=mtrdepositshare>0.01
replace has_deposits=. if mi(mtrdepositshare)
label var has_deposits "Local Deposits"
gen treated_has_deposits=has_deposits*treated
label var treated_has_deposits "Local Deposits x Any Policy"

gen x=ln(loans_mtr_lag3+1) if year==minpolicyyear
bysort ultimateparentid: egen mtr_int=max(x)
replace mtr_int=0 if mi(mtr_int)
gen treated_mtr_int=treated*mtr_int
label var treated_mtr_int "Ln MTR Loan Intensity x Anti-MTR Policy"
drop x mtr_int

gen y=loans_mtr_lag3/loans_total_lag3 if year==minpolicyyear
bysort ultimateparentid: egen mtr_to_total_int=max(y)
replace mtr_to_total_int=0 if mi(mtr_to_total_int)
gen treated_mtr_to_total_int=treated*mtr_to_total_int
label var treated_mtr_to_total_int "Percent MTR Loans  x Anti-MTR Policy"
drop y mtr_to_total_int

xtreg loans_mtr_ln treated  log_assets asset_growth_3 loans_total_lag3_ln  loans_related_lag3_ln y_* if year>=2000 & year<=2020, fe vce(cluster ultimateparentid)
sum assets_lagged if e(sample), de
disp `r(p50)'
gen med_assets_lagged=r(p50)
gen large_bank=0
replace large_bank= 1 if (assets_lagged>r(p50))
replace large_bank=. if mi(assets_lagged)
label var large_bank "Large Lender"
gen treated_large=large_bank*treated
label var treated_large "Large Lender x Any Policy"




bysort ultimateparentid year: keep if _n==1
save loan_year_vars_dataset, replace

exit
